package com.zxkj.commonpatrol.adapt;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentManager;

import com.SuperKotlin.pictureviewer.ImagePagerActivity;
import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cunoraz.gifview.library.GifView;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.daimajia.swipe.SwipeLayout;
import com.luck.picture.lib.PictureSelector;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.CheckPointListEntity;
import com.zxkj.commonpatrol.model.entity.MyMediaEntry;
import com.zxkj.commonpatrol.model.entity.PointEvent;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.model.entity.itemVideoEntity;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.ui.DeviceEventDetailActivity;
import com.zxkj.commonpatrol.ui.PatrolSetQuestActivity;
import com.zxkj.commonpatrol.utils.MediaManager;
import com.zxkj.commonpatrol.view.VioceDialog;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CheckPointListAdapt extends BaseDelegateMultiAdapter<PointEvent, BaseViewHolder> {


     private String RouteInspectNo;
    private String PointNo;
    public MyCheckPointListCallBack mDataCallBack;
    //数据回调接口
    public interface MyCheckPointListCallBack<T> {
        void onDataSuccessCallBack( T t,T t1);
        void onDataFailCallBack(T t,T t1);
    }
    public CheckPointListAdapt(String RouteInspectNo,String PointNo) {
        super();
       this.RouteInspectNo=RouteInspectNo;
        this.PointNo=PointNo;
        setMultiTypeDelegate(new BaseMultiTypeDelegate<PointEvent>() {
            @Override
            public int getItemType(@NonNull List<? extends PointEvent> data, int position) {
                // 根据数据，自己判断应该返回的类型
                switch (data.get(position).getType()) {
                    case 1:
                        return 1;
                    case 2:
                        return 2;

                    default:
                        return  1;

                }




            }
        });
        // 第二部，绑定 item 类型
        getMultiTypeDelegate()
                .addItemType(1, R.layout.item_device)//設備
                .addItemType(2, R.layout.item_event);//事件

    }

    @Override
    protected void convert(@NotNull BaseViewHolder hepler, @Nullable PointEvent checkPointListEntity) {
        SwipeLayout swipeLayout= hepler.itemView.findViewById(R.id.swipe);
        swipeLayout.setClickToClose(true);

              if (checkPointListEntity.getType()==1){
                  TextView viewById = hepler.itemView.findViewById(R.id.tv_upcoming_name);
                  TextView viewById1 = hepler.itemView.findViewById(R.id.tv_num);

                  LayoutInflater inflater = LayoutInflater.from(getContext());

                  LinearLayout linearLayout = hepler.itemView.findViewById(R.id.ll_add_view);
                  linearLayout.removeAllViews();

                  if (checkPointListEntity.getDevices().getDevPropertys()!=null){

                      for (int i = 0; i < checkPointListEntity.getDevices().getDevPropertys().size(); i++) {
//                          textView12.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Name);
//                          textView13.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Value);
//                          TextView textView =new TextView(getContext());
//                          textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1));
//                          textView.setText("测试");

                          ConstraintLayout layout = (ConstraintLayout) inflater.inflate(
                                  R.layout.item_decive_add_view, null).findViewById(R.id.cl);

                          ImageView imageView13 = (ImageView)layout.getViewById(R.id.imageView3);
                          TextView textView12 = (TextView)layout.getViewById(R.id.textView12);
                          TextView textView13 = (TextView)layout.getViewById(R.id.textView13);
                          textView12.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Name+":");
                          textView13.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Value);

                          linearLayout.addView(layout);

                      }


                  }



                  //device
                  hepler.setText(R.id.tv_upcoming_name,checkPointListEntity.getDevices().DevName);
                  hepler.setText(R.id.tv_num,checkPointListEntity.getDevices().NomalQuestCount+"/"+checkPointListEntity.getDevices().QuestTotal);


                  hepler.setText(R.id.tv_pickup_time,checkPointListEntity.getDevices().DevLocation);//位置


                  hepler.setText(R.id.tv_xhd,checkPointListEntity.getDevices().LastPatrolUser);//name
                  hepler.setText(R.id.tv_titles,"上次巡查時間: "+checkPointListEntity.getDevices().LastPatrolDateTime);//上傳巡檢時間

                  if (checkPointListEntity.getDevices().getDevPatrolResult()==0){
                      viewById.setTextColor(getContext().getColor(R.color.red));
                      viewById1.setTextColor(getContext().getColor(R.color.red));
                  }else{
                      viewById.setTextColor(getContext().getColor(R.color.green));
                      viewById1.setTextColor(getContext().getColor(R.color.green));
                  }

              }else{
                  TextView viewById = hepler.itemView.findViewById(R.id.tv_upcoming_name);
                  TextView viewById1 = hepler.itemView.findViewById(R.id.tv_num);
                  //device
                  hepler.setText(R.id.tv_upcoming_name,checkPointListEntity.getEvents().getName());
                  hepler.setText(R.id.tv_num,checkPointListEntity.getEvents().NomalQuestCount+"/"+checkPointListEntity.getEvents().QuestTotal);
                  hepler.setText(R.id.tv_address,checkPointListEntity.getEvents().getCode());//事件編號
                  hepler.setText(R.id.tv_to_address,checkPointListEntity.getEvents().getQuestsName());//問題集
                  hepler.setText(R.id.tv_pickup_time,checkPointListEntity.getEvents().EventLocation);//位置


                  hepler.setText(R.id.tv_xhd,checkPointListEntity.getEvents().LastPatrolUser);//name
                  hepler.setText(R.id.tv_titles,"上次巡查時間: "+checkPointListEntity.getEvents().LastPatrolDateTime);//上傳巡檢時間

                  if (checkPointListEntity.getEvents().getEvtPatrolResult()==0){
                      viewById.setTextColor(getContext().getColor(R.color.red));
                      viewById1.setTextColor(getContext().getColor(R.color.red));
                  }else{
                      viewById.setTextColor(getContext().getColor(R.color.green));
                      viewById1.setTextColor(getContext().getColor(R.color.green));
                  }

              }

        swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                hepler.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                hepler.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {
                hepler.itemView.findViewById(R.id.cl).setEnabled(true);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });
        hepler.itemView.findViewById(R.id.cl).setOnClickListener(v->{
                //decive   or event
            if (checkPointListEntity.getType()==1){
                Intent intent =new Intent(getContext(), DeviceEventDetailActivity.class);
                intent.putExtra("Type",1);
                intent.putExtra("RouteInspectNo",RouteInspectNo);
                intent.putExtra("PointNo",PointNo);
                intent.putExtra("name",checkPointListEntity.getDevices().DevName);
                intent.putExtra("DevEvtNo",checkPointListEntity.getDevices().DevCode);
                intent.putExtra("QuestsNo",checkPointListEntity.getDevices().QuestsNo);
                getContext().startActivity(intent);
            }else{
                Intent intent =new Intent(getContext(), DeviceEventDetailActivity.class);
                intent.putExtra("Type",2);
                intent.putExtra("name",checkPointListEntity.getEvents().getName());
                intent.putExtra("RouteInspectNo",RouteInspectNo);
                intent.putExtra("PointNo",PointNo);
                intent.putExtra("DevEvtNo",checkPointListEntity.getEvents().getCode());
                intent.putExtra("QuestsNo",checkPointListEntity.getEvents().QuestsNo);
                getContext().startActivity(intent);
            }


        });
        hepler.itemView.findViewById(R.id.rl_Delete).setOnClickListener(v -> {
            swipeLayout.close();
            mDataCallBack.onDataSuccessCallBack(checkPointListEntity.getType(),hepler.getLayoutPosition());
        });

    }

    public void setmDataCallBack(MyCheckPointListCallBack<Integer> mDataCallBack) {
        this.mDataCallBack = mDataCallBack;
    }
}
